package com.nf.health.app.intervention_details.adapter;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.nf.health.app.R;
import com.nf.health.app.adapter.ViewHolder;
import com.nf.health.app.customview.CircleImageView;
import com.nf.health.app.models.SchemeHistoryBean;
import com.nf.health.app.utils.ImageLoaderUtils;

import java.math.BigDecimal;
import java.util.List;

/**
 * Created by 恩福 on 2016/11/10.
 * 历史方案Adapter
 */

public class HistorySchemeAdapter extends BaseAdapter {

    List<SchemeHistoryBean> newsList;
    Activity activity;
    LayoutInflater inflater = null;
    public HistorySchemeAdapter(Activity activity, List<SchemeHistoryBean> newsList) {
        this.activity = activity;
        this.newsList = newsList;
        inflater = LayoutInflater.from(activity);
    }
    @Override
    public int getCount() {
        return newsList == null ? 0 : newsList.size();
    }

    @Override
    public Object getItem(int position) {
        if (newsList != null && newsList.size() != 0) {
            return newsList.get(position);
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        SchemeHistoryBean item = newsList.get(position);
        ViewHolder holder = ViewHolder.get(parent, convertView, R.layout.item_scheme_history, position);
        CircleImageView circleImageView = holder.getView(R.id.history_scheme_hing);
        TextView name = holder.getView(R.id.history_scheme_name);
        TextView doctor_name = holder.getView(R.id.history_scheme_doctor);
        TextView rq = holder.getView(R.id.history_scheme_rq);
        LinearLayout gq = holder.getView(R.id.history_scheme_gq);

        ImageLoaderUtils.getinstance(activity).getImage(circleImageView, item.getHimg(), R.drawable.default_head);
        name.setText(item.getPackname());
        doctor_name.setText(item.getDoctorname());
        if (!(Float.parseFloat(item.getEndtime()) > 0)) {
            gq.setVisibility(View.VISIBLE);
            rq.setVisibility(View.GONE);
        } else if((Float.parseFloat(item.getEndtime()) > 0) && Float.parseFloat(item.getEndtime()) < 86400000){
            rq.setText("1天后到期");
            rq.setVisibility(View.VISIBLE);
            gq.setVisibility(View.GONE);
        }else{
            gq.setVisibility(View.GONE);
            Float time = Float.parseFloat(item.getEndtime()) / 86400000;
            rq.setText(new BigDecimal(time).setScale(0, BigDecimal.ROUND_HALF_UP) + "天后到期");
        }
        return holder.getConvertView();
    }

}
